<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="d" uri="http://dgg.net/jsp/jstl/core" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>处理中订单</title>
    <c:import url="../../header.jsp"/>
</head>
<body>
<div class="content-warp">
    <div class="screen-tab-warp">
        <ul class="screen-tab">
            <li class="active tab-switch"  data-target="0"><a href="javascript:void(0)">部门提成汇总</a></li>
            <li class="tab-switch" data-target="1"><a href="javascript:void(0)">部门提成明细</a></li>
        </ul>
    </div>
    <div id="inProcessPage"></div>
</div>
</div>
<c:import url="../../footer.jsp"/>
<script>
    var processingOrder = {
        "deptAllPage":sysInfo.basePath+"/rzsc/royalty_management/dept_all.html",
        "deptDetailPage":sysInfo.basePath+"/rzsc/royalty_management/dept_detail.html"

    };
    var tables;
    dggui.use(['table', 'select2','distpicker','dggdate'], function (){
        var status=$(".active").attr("data-target");
        var table = dggui.table;
        var select2 = dggui.select2;
        var distpicker=dggui.distpicker;
        var dggdate = dggui.dggdate;
        //初始化加载表格
        deptAll();

        $(".tab-switch").click(function () {
            var target = $(this).attr("data-target");
            $(".tab-switch").each(function () {
                if (target == $(this).attr("data-target")) {
                    $(this).addClass("active");
                } else {
                    $(this).removeClass("active");
                }
            })
            switch ($(".active").attr("data-target")) {
                case "0":
                    dggui.dialog.loading();
                    deptAll();
                    break;
                case "1":
                    dggui.dialog.loading();
                    deptDetail();
                    break;
            }
        })

        //部门提成汇总
        function deptAll() {
            $.ajax({
                url: processingOrder.deptAllPage,
                data:{},
                dataType: 'html',
                success: function (html) {
                    dggui.dialog.loading('close');
                    $("#inProcessPage").html(html);
                    // 加载部门下拉框
                    ajaxGetDept("#userOrg");
                    select2.render({
                        placeholder: "请选择",
                        allowClear: true,
                        elem: $('#userOrg'),
                        width: "160px"
                    });
                    //初始化流程人员
                    initUser("RZSC_FLOW_USER","department","#flowUserId");
                    select2.render({
                        placeholder: "请选择",
                        allowClear: true,
                        elem: $('#flowUserId'),
                        width: "160px"
                    });
                    //初始化时间
                    dggdate.render({
                        elem: '#royaltyTime',
                        type: 'date',
                        range: '~'
                    });
                    getTotalDeptAll();
                    var opt={
                        "elem":"#deptAll",
                        "ajax": sysInfo.basePath + "/rzsc/royalty_management/dept_all_list.do",
                        "scrollX": "true",//是否开启横项滚动
                        "paging": "true",
                        "switchStatus": "deptAll",
                        "fixedColumns": {//锁定列，只有开启scrollX时才生效
                            "leftColumns": 1,//左侧锁定2列
                            "rightColumns": 1//右侧锁定1列
                        },
                        "fnServerParams": function (aoData) {
                            //搜索条件
                            aoData = $.extend(aoData, {
                                target:$(".active").attr("data-target"),
                                royaltyTime:$("#royaltyTime").val(),
                                userOrg:$("#userOrg").val(),
                                flowUserId:$("#flowUserId").val()
                                }
                            )
                            return aoData;
                        },
                        "aoColumns": [
                            {
                                'title': '流程人员',
                                'data': "realName",
                                "orderable": false,
                                "render": function (data, type, row, meta) {
                                    return data?data:'';
                                }
                            },
                            {
                                "title": "工号",
                                "data": "loginName",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data : '';
                                }
                            },
                            {
                                "title": "部门",
                                "data": "orgName",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data : '';
                                }
                            },
                            {
                                "title": "提成总笔数",
                                "data": "retJson.amount",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data : 0;
                                }
                            },
                            {
                                "title": "结项内部成本（元）",
                                "data": "retJson.BUS_YT_DK_nbcb",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data.toFixed(2) : 0;
                                }
                            },
                            {
                                "title": "自开发利润（元）",
                                "data": "retJson.BUS_YT_DK_zkflr",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data.toFixed(2) : 0;
                                }
                            },
                            {
                                "title": "返点利润（元）",
                                "data": "retJson.BUS_YT_DK_fdlr",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data.toFixed(2) : 0;
                                }
                            },
                            {
                                "title": "提成总金额（元）",
                                "data": "retJson.performance",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data.toFixed(2) : 0;
                                }
                            }
                        ]
                    };
                    tables=table.render(opt);
                    // 处理dataTable 异常错误信息
                    $.fn.dataTable.ext.errMode = 'none';//不抛出异常
                    tables.on( 'error.dt', function ( e, settings, techNote, message ) {
                       if(checkEmpty(settings.jqXHR.responseJSON.error)){ dggui.dialog.errors(settings.jqXHR.responseJSON.error, 2000);}
                    });
                }
            });
        }

        //部门提成明细
        function deptDetail() {
            $.ajax({
                url: processingOrder.deptDetailPage,
                dataType: 'html',
                success: function (html) {
                    dggui.dialog.loading('close');
                    $("#inProcessPage").html(html);
                    // 加载部门下拉框
                    ajaxGetDept("#userOrg");
                    //初始化流程人员
                    initUser("RZSC_FLOW_USER","department","#flowUserId");
                    select2.render({
                        placeholder: "请选择",
                        allowClear: true,
                        elem: $('#flowUserId'),
                        width: "160px"
                    });
                    select2.render({
                        placeholder: "请选择",
                        allowClear: true,
                        elem: $('#userOrg'),
                        width: "160px"
                    });
                    //初始化时间
                    dggdate.render({
                        elem: '#royaltyTime',
                        type: 'date',
                        range: '~'
                    });
                    select2.render({
                        placeholder: "请选择",
                        allowClear: true,
                        elem: $('#productCode'),
                        width: "160px"
                    });
                    select2.render({
                        placeholder: "请选择",
                        allowClear: true,
                        elem: $('#channelCode'),
                        width: "160px"
                    });
                    //初始化业务类型及产品
                    distpicker.render({
                        url: "${basePath}/rzsc/common/tree_book_list.do?code=BUS_YT_DK&type=2&level=2", // 请求地址
                        elem: '#typeCode', // 容器Id
                        type: 2,
                        placeholder: '选择业态',
                        defaultParentCode: 'BUS_YT_DK',
                        callback: function () {//如果不需要选产品就把callback整个方法注释掉
                            var code = $("#typeCode .plug-in-code").val();
                            initProductCode("#productCode", code);
                            select2.render({
                                placeholder: "请选择",
                                allowClear: true,
                                elem: $('#productCode'),
                                width: "160px"
                            });
                            var produtId = $("#productCode").val();
                            initChannel("#channelCode", produtId);
                            select2.render({
                                placeholder: "请选择",
                                allowClear: true,
                                elem: $('#channelCode'),
                                width: "160px"
                            });
                        },
                        callbackClear: function () {
                            $("#productCode").find("option").remove();
                            $("#productCode").append("<option value=''>请选择</option>");
                            $("#channelCode").find("option").remove();
                            $("#channelCode").append("<option value=''>请选择</option>");
                        }
                    });
                    //渠道初始化
                    $('#productCode').on('change', function (e) {
                        var produtId = $("#productCode").val();
                        initChannel("#channelCode", produtId);
                        select2.render({
                            placeholder: "请选择",
                            allowClear: true,
                            elem: $('#channelCode'),
                            width: "160px"
                        });
                    });

                    var opt = {
                        "elem": "#deptDetail",
                        "ajax": sysInfo.basePath + "/rzsc/royalty_management/dept_detail_list.do",
                        "scrollX": "true",//是否开启横项滚动
                        "oneSelectCheck": true,//单选，同时选中复选框
                        "fixedColumns": {//锁定列，只有开启scrollX时才生效
                            "leftColumns": 1,//左侧锁定2列
                            "rightColumns": 1//右侧锁定1列
                        },
                        "visible": true,
                        "fnServerParams": function (aoData) {
                            //搜索条件
                            aoData = $.extend(aoData, {
                                    smallBusiness: $("#typeCode .plug-in-code").val(),
                                    productId: $("#productCode").val(),
                                    channelId: $("#channelCode").val(),
                                    royaltyTime:$("#royaltyTime").val(),
                                    productOrderNo: $("#productOrderNo").val(),
                                    userOrg: $("#userOrg").val(),
                                    flowUserId: $("#flowUserId").val()
                                }
                            )
                            return aoData;
                        },
                        "aoColumns": [
                            {
                                "title": "流程人员",
                                "data": "payUserName",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data : '';
                                }
                            },
                            {
                                "title": "流水号",
                                "data": "payNo",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data : '';
                                }
                            },
                            {
                                "title": "认款时间",
                                "data": "amountMonthStr",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data : '';
                                }
                            },
                            {
                                "title": "生产事业部",
                                "data": "productOrderOrgName",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data : '';
                                }
                            },
                            {
                                "title": "部门",
                                "data": "productOrderUserOrgName",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data : '';
                                }
                            },
                            {
                                "title": "生产单编号",
                                "data": "productOrderNo",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data : '';
                                }
                            },
                            {
                                "title": "产品",
                                "data": "productName",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data : '';
                                }
                            },
                            {
                                "title": "商品",
                                "data": "commodityName",
                                "orderable": false,
                                "render": function (data, type, full, meta) {
                                    return data ? data : '';
                                }
                            }
                            ,{
                                'title': '渠道',
                                "data": "channelName",
                                "orderable": false,
                                "render": function(data, type, row, meta) {
                                    return data ? data : '';
                                }
                            } ,{
                                'title': '结项内部成本（元）',
                                "data": "calculationItem.BUS_YT_DK_nbcb",
                                "orderable": false,
                                "render": function(data, type, row, meta) {
                                    return data ? data.toFixed(2) : 0;
                                }
                            }
                            ,{
                                'title': '自开发利润金额（元）',
                                "data": "calculationItem.BUS_YT_DK_zkflr",
                                "orderable": false,
                                "render": function(data, type, row, meta) {
                                    return data ? data.toFixed(2) : 0;
                                }
                            } ,{
                                'title': '返点利润金额（元）',
                                "data": "calculationItem.BUS_YT_DK_fdlr",
                                "orderable": false,
                                "render": function(data, type, row, meta) {
                                    return data ? data.toFixed(2) : 0;
                                }
                            } ,{
                                'title': '业绩提成比例',
                                "data": "calculationItem.BUS_YT_DK_yjtcxs",
                                "orderable": false,
                                "render": function(data, type, row, meta) {
                                    return data ? data : 0;
                                }
                            } ,{
                                'title': '自开发提成比例',
                                "data": "calculationItem.BUS_YT_DK_zkftcxs",
                                "orderable": false,
                                "render": function(data, type, row, meta) {
                                    return data ? data : 0;
                                }
                            } ,{
                                'title': '提成金额（元）',
                                "data": "performanceData",
                                "orderable": false,
                                "render": function(data, type, row, meta) {
                                    return data ? data.toFixed(2) : 0;
                                }
                            }

                        ]
                    };
                    tables=table.render(opt);
                    // 处理dataTable 异常错误信息
                    $.fn.dataTable.ext.errMode = 'none';//不抛出异常
                    tables.on( 'error.dt', function ( e, settings, techNote, message ) {
                        //dggui.dialog.errors("表格查询错误，请联系管理员", 2000);
                       if(checkEmpty(settings.jqXHR.responseJSON.error)){dggui.dialog.errors(settings.jqXHR.responseJSON.error, 2000);}
                    });
                }
            });
        }
    });

    //列表刷新
    function tableReload() {
        tables.ajax.reload();
    }


    /**
     * 部门提成汇总导出
     */
    function excelExportDeptAll(url) {
        var obj = new Object();
        obj.royaltyTime=$("#royaltyTime").val();
        obj.userOrg=$("#userOrg").val();
        obj.flowUserId=$("#flowUserId").val();
        formSubmit(url, obj);
    }
    /**
     * 部门提成明细导出
     */
    function excelExportDeptDetail(url) {
        var obj = new Object();
        obj.smallBusiness=$("#typeCode .plug-in-code").val();
        obj.productId=$("#productCode").val();
        obj.channelId=$("#channelCode").val();
        obj.royaltyTime=$("#royaltyTime").val();
        obj.productOrderNo=$("#productOrderNo").val();
        obj.userOrg=$("#userOrg").val();
        obj.flowUserId=$("#flowUserId").val();
        formSubmit(url, obj);
    }

    /**
     * 获取总金额及业绩数据
     */
    function getTotalDeptAll() {
        var royaltyTime = $("#royaltyTime").val();
        var userOrg = $("#userOrg").val();
        var flowUserId = $("#flowUserId").val();
        $.ajax({
            url: sysInfo.basePath + "/rzsc/royalty_management/get_total_dept_all.do",
            data: {royaltyTime: royaltyTime,userOrg:userOrg,flowUserId:flowUserId},
            type: "POST",
            dataType: "json",
            success: function (result) {
                if (result.code == 0) {
                    var data = result.data;
                    if(isEmpty(data)){
                        $("#performance").text("0 元");
                        $("#achievement").text("0 元");
                    }else {
                        $("#performance").text(data.performance.toFixed(2)+" 元");
                        $("#achievement").text(data.achievement.toFixed(2)+" 元");
                    }
                } else {
                    dggui.dialog.errors("获取总业绩失败");
                }
            }
        });
    }

</script>
</body>
</body>
</html>